/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sands;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Admin
 */
public final class DatabaseHelper extends SQLiteOpenHelper {

    private static String dbAndTableName = "nemo" ;
    private static String url = "url";
    private static String uname = "uname";
    private static String pwd = "pwd";
    private static String remarks = "remarks";
    private static DatabaseHelper databaseHelper ;
    public DatabaseHelper(Context context, String name, CursorFactory factory, int version){
	super(context, dbAndTableName, factory, 33);
    }
    
    public static DatabaseHelper getInstance(Context c) {
	if( databaseHelper == null ) {
	    databaseHelper = new DatabaseHelper(c, "", null, 1);
	}
	return databaseHelper;
    }
    
    @Override
    public void onCreate(SQLiteDatabase db){
	try{
	    db.beginTransaction();
	    db.execSQL("CREATE TABLE IF NOT EXISTS " + dbAndTableName + " (id INTEGER PRIMARY KEY AUTOINCREMENT," + url + " TEXT, " + uname + " TEXT, " + pwd + " TEXT, "+remarks+" TEXT );");
	    db.setTransactionSuccessful();
	}
	finally{
	    db.endTransaction();
	}
    }

    public List<Nemo> getNemos() {
	List<Nemo> list = new ArrayList();
	Cursor cursor = null;
	try{
	    SQLiteDatabase db = getReadableDatabase();
	    cursor = db.rawQuery("SELECT * FROM " + dbAndTableName +" order by "+ url, null);
	    if(cursor.getCount() > 0){
		cursor.moveToFirst();
		do{
		    String url_ = cursor.getString(cursor.getColumnIndex(url));
		    String uname_ = cursor.getString(cursor.getColumnIndex(uname));
		    String pwd_ = cursor.getString(cursor.getColumnIndex(pwd));
		    String remarks_ = cursor.getString(cursor.getColumnIndex(remarks));
		    Nemo nemo = new Nemo(url_, uname_, pwd_);
		    nemo.setRemarks(remarks_);
		    list.add(nemo);
		    cursor.moveToNext();
		}
		while(!cursor.isAfterLast());
	    }
	} catch( Exception e) {
	    Log.e("GetNemos", "error", e);
	}
	finally{
	    if(cursor != null){
		cursor.close();
	    }
	    return list;
	}
    }
    
    public String deleteNemo(String url_) {
	SQLiteDatabase db = getWritableDatabase();
	String message = null;
	try{
	db.beginTransaction();
	int cnt = db.delete(dbAndTableName , url+ "=?" , new String[] {url_});
	//db.execSQL("delete from " + dbAndTableName + " where " + url + " = '" +url_+"'" );
	db.setTransactionSuccessful();
	db.endTransaction();
	message = url_ + " is deleted["+cnt+"]";
	} catch(Exception e) {
	    Log.e("DELETE NEMO","Error",e);
	    message = e.getMessage();
	} finally {
	    db.close();
	    return message;
	}
    }
    
    public String insertNemo(Nemo nemo) {
	SQLiteDatabase db = getWritableDatabase();
	ContentValues cv = new ContentValues();
	cv.put(url, nemo.getUrl());
	cv.put(uname, nemo.getuName());
	cv.put(pwd, nemo.getPwd());
	cv.put(remarks, nemo.getRemarks());
	long insert = db.insert(dbAndTableName, url, cv);
	return insert > 1 ? "Success, added at " + insert : "FAILURE";
    }
    
    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2){
	db.execSQL("DROP TABLE IF EXISTS " + dbAndTableName);
    }
    
}
